//
//  MainPageViewController.m
//  LeftSlide
//
//  Created by huangzhenyu on 15/6/18.
//  Copyright (c) 2015年 eamon. All rights reserved.
//

#import "MainPageViewController.h"
#import "AppDelegate.h"
#import "OnlineViewController.h"
#import "MusicViewController.h"

#define vBackBarButtonItemName  @"backArrow.png"    //导航条返回默认图片名
@interface MainPageViewController ()

@property (nonatomic, strong)OnlineViewController *onlineVC;
@property (nonatomic, strong)MusicViewController *musicVC;


@end

@implementation MainPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"主界面";
    self.view.backgroundColor = [UIColor whiteColor];
    // 自动调整
    self.automaticallyAdjustsScrollViewInsets = NO;
    UIButton *menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    menuBtn.frame = CGRectMake(0, 0, 20, 18);
    [menuBtn setBackgroundImage:[UIImage imageNamed:@"menu"] forState:UIControlStateNormal];
    [menuBtn addTarget:self action:@selector(openOrCloseLeftList) forControlEvents:UIControlEventTouchUpInside];
//    menuBtn.backgroundColor = [UIColor redColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:menuBtn];
//    self.navigationController.navigationBar.translucent = NO;
//    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    [self createChildVC];
    [self createView];
}

- (void)createChildVC {
    self.musicVC = [[MusicViewController alloc] init];
    self.onlineVC = [[OnlineViewController alloc] init];
    [self addChildViewController:self.musicVC];
    [self addChildViewController:self.onlineVC];
}

- (void)createView {
    UISegmentedControl *segmentControl = [[UISegmentedControl alloc] initWithItems:@[@"直播", @"音乐"]];
    self.navigationItem.titleView = segmentControl;
    [segmentControl addTarget:self action:@selector(segmentControl:) forControlEvents:UIControlEventValueChanged];
    segmentControl.selectedSegmentIndex = 1;
    [segmentControl setTitle:@"直播" forSegmentAtIndex:0];
    [segmentControl setTitle:@"音乐" forSegmentAtIndex:1];
    [segmentControl setWidth:90 forSegmentAtIndex:0];
    [segmentControl setWidth:90 forSegmentAtIndex:1];
    segmentControl.layer.cornerRadius = 15;
    segmentControl.layer.masksToBounds = YES;
//    segmentControl.layer.borderColor = (__bridge CGColorRef _Nullable)([UIColor yellowColor]);
    [self.view addSubview:self.musicVC.view];
}
- (void)segmentControl:(UISegmentedControl *)segment {
    if (segment.selectedSegmentIndex == 1) {
        if (![self.musicVC.view isDescendantOfView:self.view]) {
            [self.view addSubview:self.musicVC.view];
        }
        [self.view bringSubviewToFront:self.musicVC.view];
    } else {
        if (![self.onlineVC.view isDescendantOfView:self.view]) {
            [self.view addSubview:self.onlineVC.view];
        }
        [self.view bringSubviewToFront:self.onlineVC.view];
    }
}

- (void) openOrCloseLeftList
{
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    if (tempAppDelegate.leftSlideVC.closed)
    {
        [tempAppDelegate.leftSlideVC openLeftView];
    }
    else
    {
        [tempAppDelegate.leftSlideVC closeLeftView];
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    NSLog(@"viewWillDisappear");
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [tempAppDelegate.leftSlideVC setPanEnabled:NO];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSLog(@"viewWillAppear");
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [tempAppDelegate.leftSlideVC setPanEnabled:YES];
}



@end
